package com.ruoyi.web.controller.erp;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.common.enums.OrderProductStatus;
import com.ruoyi.erp.common.enums.OrderStatus;
import com.ruoyi.erp.domain.XzgOrderProduct;
import com.ruoyi.erp.domain.dto.XzgOrderExcelDto;
import com.ruoyi.erp.domain.dto.XzgOrderProductDto;
import com.ruoyi.erp.domain.dto.XzgOrderProductExcelDto;
import com.ruoyi.erp.domain.vo.XzgOrderProductVo;
import com.ruoyi.erp.domain.vo.XzgOrderVo;
import com.ruoyi.erp.service.IXzgOrderProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author qiuwencong
 * @Date 2022/1/4 16:58
 * @Package com.ruoyi.web.controller.erp
 * @Description
 */
@RestController
@RequestMapping("/erp/orderProduct")
public class OrderProductController extends BaseController {

    @Autowired
    private IXzgOrderProductService xzgOrderProductService;

    /**
     * 添加订单产品
     *
     * @param product
     * @return
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody XzgOrderProduct product) {
        product.setCreateBy(getUsername());
        product.setUpdateBy(getUsername());
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        return toAjax(xzgOrderProductService.insertOrderProduct(product));
    }

    /**
     * 获取订单产品列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(XzgOrderProductDto dto) {
        startPage();
        List<XzgOrderProductVo> orderList = xzgOrderProductService.selectOrderProductList(dto);
        return getDataTable(orderList);
    }

    /**
     * 根据id获取订单产品
     *
     * @param orderProductId
     * @return
     */
    @GetMapping("/{orderProductId}")
    public AjaxResult getById(@PathVariable Long orderProductId) {
        return AjaxResult.success(xzgOrderProductService.selectOrderProductById(orderProductId));
    }

    /**
     * 根据id修改订单产品
     *
     * @param product
     * @return
     */
    @PutMapping
    public AjaxResult updateOrderProduct(@RequestBody XzgOrderProduct product) {
        product.setUpdateBy(getUsername());
        product.setUpdateTime(new Date());
        product.setOrderId(null);
        return toAjax(xzgOrderProductService.updateOrderProduct(product));
    }

    /**
     * 根据id逻辑删除
     *
     * @param orderProductIds
     * @return
     */
    @DeleteMapping("/{orderProductIds}")
    public AjaxResult delete(@PathVariable Long[] orderProductIds) {
        return toAjax(xzgOrderProductService.deleteOrderProductById(orderProductIds, getUsername()));
    }

    /**
     * 下载模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<XzgOrderProductExcelDto> util = new ExcelUtil<>(XzgOrderProductExcelDto.class);
        util.importTemplateExcel(response, "订单产品数据");
    }

    /**
     * 导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport, Long orderId) throws Exception {
        ExcelUtil<XzgOrderProductExcelDto> util = new ExcelUtil<>(XzgOrderProductExcelDto.class);
        List<XzgOrderProductExcelDto> orderProducts = util.importExcel(file.getInputStream());
        return AjaxResult.success(xzgOrderProductService.importOrderProducts(orderProducts, updateSupport, orderId, getUsername()));
    }

    /**
     * 导出
     *
     * @param response
     * @param dto
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, XzgOrderProductDto dto) {
        List<XzgOrderProductVo> xzgOrderProductVos = xzgOrderProductService.selectOrderProductList(dto);
        for (XzgOrderProductVo xzgOrderProductVo : xzgOrderProductVos) {
            xzgOrderProductVo.setStatus(OrderProductStatus.getValueByKey(xzgOrderProductVo.getStatus()));
        }
        ExcelUtil<XzgOrderProductVo> util = new ExcelUtil<>(XzgOrderProductVo.class);
        util.exportExcel(response, xzgOrderProductVos, "订单产品数据");
    }
}
